//
//  TotalRecordSelectedView.m
//  EasyToTesco2.0
//
//  Created by baozi on 16/8/16.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "TotalRecordSelectedView.h"

#define color [UIColor colorWithRed:181/255. green:180/255. blue:180/255. alpha:1]

@interface TotalRecordSelectedView ()

@property (nonatomic,strong)NSMutableArray *buttonArray;

@property (nonatomic,strong)UIView *lineview;

@end

@implementation TotalRecordSelectedView

- (NSMutableArray *)buttonArray {
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpTotalRecordSelectedView];
    }
    return self;
}

- (void)setUpTotalRecordSelectedView {
    UIView *bottom = [[UIView alloc] init];
    bottom.backgroundColor = color;
    bottom.alpha = 0.1;
    [self addSubview:bottom];
    
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(1);
    }];
    
    CGFloat width = (WIDTH - 2) / 3;
    NSArray *array = @[@"积分权",@"代金券",@"活动"];
    for (int i = 0; i < array.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i + 1000;
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        button.titleLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(18.0)];
        
        [button setFrame:CGRectMake(i * width + i, 0, width, self.Height)];
        
        [button addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:color forState:UIControlStateNormal];
        if (i==0) {
            [button setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
        }
        
        [button setTitle:array[i] forState:UIControlStateNormal];
        [self.buttonArray addObject:button];
        [self addSubview:button];
    }
    
    UIView *bottomXView = [[UIView alloc] init];
    bottomXView.backgroundColor = DEFAULT_COLOR;
    bottomXView.alpha = 0.5;
    [self addSubview:bottomXView];
    
    [bottomXView setFrame:CGRectMake(20, self.Height - 4, width - 40, 4)];
    
    self.lineview = bottomXView;
    
    UIView *leftLine = [[UIView alloc] init];
    leftLine.backgroundColor = color;
    leftLine.alpha = 0.1;
    [self addSubview:leftLine];
    
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(width);
        make.top.equalTo(self.mas_top).with.offset(3);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(HEIGHT * 0.07 - 6);
    }];
    
    UIView *rightLine = [[UIView alloc] init];
    rightLine.backgroundColor = color;
    rightLine.alpha = 0.1;
    [self addSubview:rightLine];
    
    [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftLine.mas_right).with.offset(width);
        make.top.equalTo(self.mas_top).with.offset(3);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(HEIGHT * 0.07 - 6);
    }];
}

- (void)selectButtonClick:(UIButton *)button {
    [UIView animateWithDuration:0.5 animations:^{
        
        CGPoint labelC = self.lineview.center;
        labelC = button.center;
        self.lineview.center = labelC;
        
        CGRect labelF = self.lineview.frame;
        labelF.origin.y = self.frame.size.height - labelF.size.height;
        self.lineview.frame = labelF;

    }];
    
    for (UIButton *tempBtn in self.buttonArray) {
        
        [tempBtn setTitleColor:color forState:UIControlStateNormal];
        if (!tempBtn.selected) {
            [button setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(selectBtnClick:)]) {
        [self.delegate selectBtnClick:button];
    }
}

@end
